package com.clss.commandhelper.function.homefragment3rd;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.clss.commandhelper.R;
import com.clss.commandhelper.base.BaseFragment;
import com.clss.commandhelper.tools.Constant;
import com.clss.commandhelper.tools.Lg;
import com.clss.commandhelper.tools.SpHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuv
 * @date 2017/12/6
 * 应用
 */

public class Home3rdFragment extends BaseFragment implements AdapterView.OnItemClickListener{
    private static final String TAG = "Home3rdFragment";

    private final FunctionTag ReleaseInform = new FunctionTag(R.mipmap.icon_release_inform, "发布通告", 0);
    private final FunctionTag Callboard = new FunctionTag(R.mipmap.icon_callboard, "通告列表", 1);
    private final FunctionTag Messenger = new FunctionTag(R.mipmap.icon_messenger, "信息员列表", 2);
    private final FunctionTag Clue = new FunctionTag(R.mipmap.home_tab_2nd_true, "线索列表", 3);

    private GridView mFunctionGv;
    private FunctionItemAdapter mFunctionItemAdapter;
    private List<FunctionTag> mFunctionList = new ArrayList<>();

    @Override
    protected int setViewId() {
        return R.layout.fragment_home3rd;
    }

    @Override
    protected void initView(View v) {
        mFunctionGv = v.findViewById(R.id.fragment_home3rd_gv);
        mFunctionItemAdapter = new FunctionItemAdapter(getContext(), 0);

        mFunctionGv.setAdapter(mFunctionItemAdapter);
        mFunctionGv.setOnItemClickListener(this);

        switch (SpHelper.getInstance().getRole()){
            case Constant.OFFICER:
                mFunctionList.add(ReleaseInform);
                mFunctionList.add(Callboard);
                mFunctionList.add(Messenger);
                mFunctionList.add(Clue);
                break;
            default:
        }
        mFunctionItemAdapter.notifyDataSetChanged();
    }

    @Override
    protected void initData(Bundle savedInstanceState) {

    }

    @Override
    public void responseError(int code, String msg) {

    }

    @Override
    public void requestFailure(Throwable t) {

    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        final FunctionTag itemTag = mFunctionList.get(i);
        switch (itemTag.tag){
            case 0:
                Lg.i(TAG, "---onItemClick===" + itemTag.title);

                break;
            case 1:
                Lg.i(TAG, "---onItemClick===" + itemTag.title);

                break;
            case 2:
                Lg.i(TAG, "---onItemClick===" + itemTag.title);

                break;
            case 3:
                Lg.i(TAG, "---onItemClick===" + itemTag.title);

                break;
            default:
        }
    }

    private class FunctionItemAdapter extends ArrayAdapter {
        public FunctionItemAdapter(@NonNull Context context, int resource) {
            super(context, resource);
        }

        @Override
        public int getCount() {
            return mFunctionList.size();
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            @SuppressLint("ViewHolder") View view = getLayoutInflater().inflate(R.layout.item_function, parent, false);
            ImageView iconIv = view.findViewById(R.id.item_function_icon_iv);
            TextView titleTv = view.findViewById(R.id.item_function_tv);
            iconIv.setImageResource(mFunctionList.get(position).icon);
            titleTv.setText(mFunctionList.get(position).title);
            return view;
        }
    }

    private class FunctionTag{
        public FunctionTag(int icon, String title, int tag) {
            this.icon = icon;
            this.title = title;
            this.tag = tag;
        }

        public int icon;
        public String title;
        public int tag;
    }
}
